package com.fpost.regex.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RegexUtils {
	
	private static final char[] SPECIAL_CHARS = 
		{
			0x0060, // `
			0x007e, // ~
			0x0021, // !
			0x0040, // @
			0x0023, // #
			0x0024, // $
			0x0025, // %
			0x005e, // ^
			0x0026, // &
			0x002a, // *
			0x0028, // (
			0x0029, // )
			0x005f, // _
			0x002d, // -
			0x002b, // +
			0x003d, // =
			0x007b, // {
			0x007d, // }
			0x007c, // |
			0x005b, // [
			0x005d, // ]
			0x005c, // \
			0x003a, // :
			0x003b, // ;
			0x0022, // "
			0x0027, // '
			0x002c, // ,
			0x002e, // .
			0x002f, // /
			0x003c, // <
			0x003e, // >
			0x003f, // ?
			0x00ab, // «
			0x00bb  // »
		};
	
	private static final Set<Character> CHAR_SET = new HashSet<Character>();
	
	static {		
		for (int i = 0; i < SPECIAL_CHARS.length; i++) {
			CHAR_SET.add(SPECIAL_CHARS[i]);
		} 
	}
	
	public static void main(String[] args) {
		
		String ukr_b = "АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ _";
		String ukr_s = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя _";		
		String rus = "ёъыэЁЪЫЭ _";
		String special_chars_str = "`~!@#$%^&*()_-+={}|[]\\:;\"',./<>?«»";

		
		String test = special_chars_str;
		char[] dst = new char[test.length()];
		test.getChars(0, test.length(), dst, 0);
		
		for (char c : dst) {
			//System.out.println(c + "(\\u"+UnicodeFormatter.charToHex(c)+") - " + String.valueOf(isCyrillic(c)));
			System.out.println("0x"+UnicodeFormatter.charToHex(c) + ", // " + c);

		}		
	}	
	
	private static boolean isCyrillic(char c) {
		return Character.UnicodeBlock.CYRILLIC.equals(Character.UnicodeBlock.of(c));
	}
	
	private static String removeSpecialCharacters(String input) {
		
		StringBuilder sb = new StringBuilder();
		char[] inputArray = input.toCharArray(); 
		for (char c : inputArray) {
			if (CHAR_SET.contains(c)) {
				sb.append(" ");
			} else {
				sb.append(c);
			}				
		}
		return sb.toString();
	}
	
	public static List<String> cleanInputUTF(String input) {

		String[] strArray = removeSpecialCharacters(input).split(" ");
		List<String> list = Arrays.asList(strArray);
		
		//remove strings composed of spaces (duplicates allowed)
		List<String> temp = new ArrayList<String>();		
		for (String string : list) {
			string = string.trim();
			if (!"".equals(string)) temp.add(string);
		}		
		return temp;
	}
	
	@Deprecated
	public static List<String> cleanInput(String input) {		

		String[] strArray = input.replaceAll("\\W", " ").split(" ");
		List<String> list = Arrays.asList(strArray);
		
		//remove strings composed of spaces (duplicates allowed)
		List<String> temp = new ArrayList<String>();		
		for (String string : list) {
			string = string.trim();
			if (!"".equals(string)) temp.add(string);
		}		
		return temp;
	}
	
	public static Set<String> cleanArticleContentUTF(String articleContent) {
		
		String[] strArray = removeSpecialCharacters(articleContent).split(" ");
		List<String> list = Arrays.asList(strArray);
		
		//remove duplicates
		Set<String> set = new HashSet<String>(list);
		
		//remove strings composed of spaces 
		Set<String> temp = new HashSet<String>(list);
		for (String string : set) {
			string = string.trim();
			if (!"".equals(string)) temp.add(string);
		}		
		return temp;
	}
	
	@Deprecated
	public static Set<String> cleanArticleContent(String articleContent) {		

		String[] strArray = articleContent.replaceAll("\\W", " ").split(" ");
		List<String> list = Arrays.asList(strArray);
		
		//remove duplicates
		Set<String> set = new HashSet<String>(list);
		
		//remove strings composed of spaces 
		Set<String> temp = new HashSet<String>(list);
		for (String string : set) {
			string = string.trim();
			if (!"".equals(string)) temp.add(string);
		}		
		return temp;
	}

}
